import { createRouter, createWebHashHistory } from "vue-router"


import Home from "../views/Home.vue"
import ItemMusic from "../views/ItemMusic.vue"
import Testcom from '../views/Testcom.vue'
import SearchFor from '../views/SearchFor.vue'
import Login from '../views/Login.vue'


const router = createRouter({
    history: createWebHashHistory(),
    routes: [{
        path: "/",
        name: "home",
        label: "Home",
        component: Home,
    },
    {
        path: "/itemmusic",
        name: "itemmusic",
        label: "ItemMusic",
        component: ItemMusic,
    },
    {
        path: "/testcom",
        name: "testcom",
        label: "Testcom",
        component: Testcom,
    },
    {
        path: "/searchfor",
        name: "searchfor",
        label: "SearchFor",
        component: SearchFor,
    },
    {
        path: "/login",
        name: "login",
        label: "Login",
        component: Login,
    }
    ],
});


//前置守卫
import {useStore} from '../stores/index'   //把这个userId获取过来


router.beforeEach((to,from,next)=>{
    const  store=useStore()
	if(to.name!=="login"&&to.name!=="home"){
		if(!store.token){
		// 	next()
		// }else{
			next({path:'/login'})
		}else{
            next()
        }
	}else{
		next()
	}
})



export default router;